<?php

if(!defined('FYSCU_ROOT')){
	exit('非法访问');
}

class index extends action_base{
	public static $num = 5;
	public function action_index(){
		$row = FYDB::find_all('kp_word_main', array('id','dateline','word','des','example'), array(
			'where'=>1,
			'data'=>array(),
			'order'=>'id desc',
			'limit'=>self::$num,
		));
		foreach ($row as $k=>$v){
			$row[$k]['day'] = date('m/d',$v['dateline']);
			$row[$k]['val'] = FYDB::find_clumn('kp_word_p', array('up','down'), array(
			'where'=>'fid=:f',
			'data'=>array(':f'=>$v['id']),
			));
		}
		
		$this->render('index',array('data'=>$row,'sign'=>$sign));
	}
	
	public function action_ajax(){
		
		if(isset($_POST['op'])){
			$op = $_POST['op']['op'];
			$id = $_POST['op']['id'];
			if(!$this->time_limit($id)){
				die('-2');
			}
			FYDB::update('kp_word_p', array($op=>$op.'+1'), array(
			'where'=>'id=:i',
			'data'=>array(':i'=>$id),
			));
			die('1');
		}else{
			die('-1');
		}
	}
	
	public function action_loadmore()
	{
		
		$last = FYTOOL::get_gp_value('id');
		if(is_numeric($last)){
			$row = FYDB::find_all('kp_word_main', array('id','dateline','word','des','example'), array(
				'where'=>'id<:i',
				'data'=>array(':i'=>$last),
				'order'=>'id desc',
				'limit'=>self::$num,
			));
			foreach ($row as $k=>$v){
				$row[$k]['day'] = date('m/d',$v['dateline']);
				$row[$k]['val'] = FYDB::find_clumn('kp_word_p', array('up','down'), array(
				'where'=>'fid=:f',
				'data'=>array(':f'=>$v['id']),
				));
			}
			
			die(json_encode(array('data'=>$row,'last'=>$v['id'])));
		}else{
			die('-1 last err');
		}
		
	}
	
	private function time_limit($id)
	{
		$t = time();
		if(isset($_SESSION['last'][$id])){
			if(($t-$_SESSION['last'][$id])>60){
				$_SESSION['last'][$id] = $t;
				return true;
			}else{
				return false;
			}
		}else{
			$_SESSION['last'][$id] = $t;
			return true;
		}
	}
	
	
}
?>